package com.zhouxiaoxuan13.consumer.feign.fallback;

import com.zhouxiaoxuan13.consumer.dto.FreshProduct;
import com.zhouxiaoxuan13.consumer.feign.FreshProductServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 生鲜商品服务Feign客户端降级处理类
 * 当生鲜商品服务不可用时提供默认响应
 */
@Slf4j
@Component
public class FreshProductServiceFallback implements FreshProductServiceClient {

    @Override
    public ResponseEntity<List<FreshProduct>> getAllFreshProducts() {
        log.warn("生鲜商品服务不可用，返回降级响应 - getAllFreshProducts");
        List<FreshProduct> fallbackList = new ArrayList<>();
        FreshProduct fallbackProduct = createFallbackProduct();
        fallbackList.add(fallbackProduct);
        return ResponseEntity.ok(fallbackList);
    }

    @Override
    public ResponseEntity<FreshProduct> getFreshProductById(Long id) {
        log.warn("生鲜商品服务不可用，返回降级响应 - getFreshProductById: {}", id);
        FreshProduct fallbackProduct = createFallbackProduct();
        fallbackProduct.setId(id);
        return ResponseEntity.ok(fallbackProduct);
    }

    @Override
    public ResponseEntity<List<FreshProduct>> searchFreshProducts(String name) {
        log.warn("生鲜商品服务不可用，返回降级响应 - searchFreshProducts: {}", name);
        List<FreshProduct> fallbackList = new ArrayList<>();
        FreshProduct fallbackProduct = createFallbackProduct();
        fallbackProduct.setName("搜索服务暂时不可用: " + name);
        fallbackList.add(fallbackProduct);
        return ResponseEntity.ok(fallbackList);
    }

    @Override
    public ResponseEntity<List<FreshProduct>> getFreshProductsByCategory(String category) {
        log.warn("生鲜商品服务不可用，返回降级响应 - getFreshProductsByCategory: {}", category);
        List<FreshProduct> fallbackList = new ArrayList<>();
        FreshProduct fallbackProduct = createFallbackProduct();
        fallbackProduct.setCategory(category);
        fallbackList.add(fallbackProduct);
        return ResponseEntity.ok(fallbackList);
    }

    @Override
    public ResponseEntity<List<FreshProduct>> getPromotionProducts() {
        log.warn("生鲜商品服务不可用，返回降级响应 - getPromotionProducts");
        List<FreshProduct> fallbackList = new ArrayList<>();
        FreshProduct fallbackProduct = createFallbackProduct();
        fallbackProduct.setIsOnPromotion(false);
        fallbackList.add(fallbackProduct);
        return ResponseEntity.ok(fallbackList);
    }

    @Override
    public ResponseEntity<List<FreshProduct>> getLowStockProducts(Integer threshold) {
        log.warn("生鲜商品服务不可用，返回降级响应 - getLowStockProducts: {}", threshold);
        List<FreshProduct> fallbackList = new ArrayList<>();
        FreshProduct fallbackProduct = createFallbackProduct();
        fallbackProduct.setStockQuantity(0);
        fallbackList.add(fallbackProduct);
        return ResponseEntity.ok(fallbackList);
    }

    @Override
    public ResponseEntity<FreshProduct> createFreshProduct(FreshProduct freshProduct) {
        log.warn("生鲜商品服务不可用，无法创建商品 - createFreshProduct");
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
    }

    @Override
    public ResponseEntity<FreshProduct> updateFreshProduct(Long id, FreshProduct freshProductDetails) {
        log.warn("生鲜商品服务不可用，无法更新商品 - updateFreshProduct: {}", id);
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
    }

    @Override
    public ResponseEntity<Void> deleteFreshProduct(Long id) {
        log.warn("生鲜商品服务不可用，无法删除商品 - deleteFreshProduct: {}", id);
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
    }

    /**
     * 创建降级商品信息
     */
    private FreshProduct createFallbackProduct() {
        FreshProduct product = new FreshProduct();
        product.setId(-1L);
        product.setName("服务暂时不可用");
        product.setDescription("生鲜商品服务正在维护中，请稍后重试");
        product.setCategory("系统");
        product.setSpecifications("N/A");
        product.setOriginalPrice(BigDecimal.ZERO);
        product.setStockQuantity(0);
        product.setExpiryDate(LocalDate.now().plusDays(1));
        product.setPromotionPrice(BigDecimal.ZERO);
        product.setIsOnPromotion(false);
        product.setPromotionId(null);
        product.setCreatedTime(LocalDateTime.now());
        product.setUpdatedTime(LocalDateTime.now());
        return product;
    }
}
